package com.koron.order.linepatrol.uav.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.BaseBean;
import com.koron.bean.query.PageQuery;
import com.koron.order.linepatrol.uav.bean.VO.LineRiskPatrolUnv;
import com.koron.order.linepatrol.uav.bean.VO.LineRiskPatrolUnvDetail;
import com.koron.order.linepatrol.uav.bean.query.LineRiskPatrolUnvDetailQuery;
import com.koron.order.linepatrol.uav.bean.query.LineRiskPatrolUnvQuery;
import com.koron.order.linepatrol.uav.mapper.LineRiskPatrolUnvDetailMapper;
import com.koron.order.linepatrol.uav.mapper.LineRiskPatrolUnvMapper;
import com.koron.order.linepatrol.uav.service.LineRiskPatrolUnvService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
*
*/
@Service
public class LineRiskPatrolUnvServiceImpl implements LineRiskPatrolUnvService {

    @Override
    @TaskAnnotation("list")
    public PageInfo<LineRiskPatrolUnv> list(SessionFactory sessionFactory, LineRiskPatrolUnvQuery lineRiskPatrolUnvQuery, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPage(),pageQuery.getPageSize(),pageQuery.getOrderBy());
        LineRiskPatrolUnvMapper mapper = sessionFactory.getMapper(LineRiskPatrolUnvMapper.class);
        List<LineRiskPatrolUnv> list = mapper.list(lineRiskPatrolUnvQuery, pageQuery);
        List<String> ids = list.stream().map(BaseBean::getId).collect(Collectors.toList());
        LineRiskPatrolUnvDetailMapper detailMapper = sessionFactory.getMapper(LineRiskPatrolUnvDetailMapper.class);
        List<LineRiskPatrolUnvDetail> lineRiskPatrolUnvDetails = detailMapper.selectList(new LineRiskPatrolUnvDetailQuery());
        for (String id : ids) {
            long reportCount = lineRiskPatrolUnvDetails.stream().filter(i -> id.equals(i.getMonitorId())
                    && "1".equals(i.getReportStatus())
                    ).count();
            long total = lineRiskPatrolUnvDetails.stream().filter(i -> id.equals(i.getMonitorId())).count();
            if(reportCount != 0 && reportCount == total){
                list.stream().filter(i->i.getId().equals(id)).forEach(i->i.setReportStatus("1"));
            }
            else if (reportCount >0) {
                list.stream().filter(i->i.getId().equals(id)).forEach(i->i.setReportStatus("3"));
            }
        }
        return new PageInfo<LineRiskPatrolUnv>(list);
    }
    @Override
    @TaskAnnotation("update")
    public Boolean update(SessionFactory sessionFactory, LineRiskPatrolUnvQuery lineRiskPatrolUnvQuery) {

        LineRiskPatrolUnvMapper mapper = sessionFactory.getMapper(LineRiskPatrolUnvMapper.class);
        Boolean update = mapper.update(lineRiskPatrolUnvQuery);
        return update;
    }

    @Override
    @TaskAnnotation("deleteLogic")
    public Boolean deleteLogic(SessionFactory sessionFactory, String id) {
        LineRiskPatrolUnvMapper mapper = sessionFactory.getMapper(LineRiskPatrolUnvMapper.class);
        Boolean deleteLogic = mapper.deleteLogic(id);
        return deleteLogic;
    }

    @Override
    @TaskAnnotation("deleteBatch")
    public Integer deleteBatch(SessionFactory sessionFactory, List<String> ids) {
        LineRiskPatrolUnvMapper mapper = sessionFactory.getMapper(LineRiskPatrolUnvMapper.class);
        Integer integer = mapper.deleteBatch(ids);
        return integer;
    }

    @Override
    @TaskAnnotation("insert")
    public Boolean insert(SessionFactory sessionFactory, LineRiskPatrolUnv lineRiskPatrolUnv) {
        LineRiskPatrolUnvMapper mapper = sessionFactory.getMapper(LineRiskPatrolUnvMapper.class);
        Boolean insert = mapper.insert(lineRiskPatrolUnv);
        return insert;
    }
}
